package week_01;

import java.util.Stack;

public class MinStack_155 {
    private Stack<Integer> dataStack;
    private Stack<Integer> minStack;

    /**
     * initialize your data structure here.
     */
    public MinStack_155() {
        this.dataStack = new Stack<>();
        this.minStack = new Stack<>();
    }

    public void push(int val) {
        dataStack.push(val);
        if (minStack.empty()) {
            minStack.push(val);
        } else {
            int min = Math.min(val, minStack.peek());
            minStack.push(min);
        }
    }

    public void pop() {
        if (dataStack.empty()) {
            throw new RuntimeException();
        } else {
            dataStack.pop();
            minStack.pop();
        }
    }

    public int top() {
        if (dataStack.empty()) {
            throw new RuntimeException();
        } else {
            return dataStack.peek();
        }
    }

    public int getMin() {
        if (dataStack.empty()) {
            throw new RuntimeException();
        } else {
            return minStack.peek();
        }
    }
}
